/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import data_access.ResultObj;

/**
 * 
 * @author Anh
 */
public class LineChart extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LineChart() {

	}

	public LineChart(ArrayList<ResultObj> resultList) {
		JFreeChart xylineChart = ChartFactory.createLineChart("Line Chart", "Test File (ClassNumber/TestingSize)",
				"Accuracy(%)", createDataset(resultList), PlotOrientation.VERTICAL, true, true, false);
		// xylineChart.setBackgroundPaint(Color.WHITE);
		ChartPanel chartPanel = new ChartPanel(xylineChart);
		chartPanel.setPreferredSize(new java.awt.Dimension(560, 367));
		final CategoryPlot plot = (CategoryPlot) xylineChart.getPlot();
		// plot.setBackgroundPaint(Color.WHITE);
		plot.setOutlineVisible(false);
		plot.setRangeGridlinePaint(Color.white);
		plot.setDomainGridlinePaint(Color.BLUE);
		LineAndShapeRenderer renderer = new LineAndShapeRenderer();

		renderer.setSeriesPaint(0, Color.RED);
		renderer.setSeriesPaint(1, Color.GREEN);
		renderer.setSeriesPaint(2, Color.BLUE);

		renderer.setSeriesStroke(0, new BasicStroke(3.0f));
		renderer.setSeriesStroke(1, new BasicStroke(3.0f));
		renderer.setSeriesStroke(2, new BasicStroke(3.0f));
		renderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());

		plot.setRenderer(renderer);
		/*
		 * NumberAxis number = (NumberAxis)plot.getDomainAxis();
		 * number.setRange(0, 16); number.setTickUnit(new NumberTickUnit(1));
		 */
		// number.setVerticalTickLabels(true);
		NumberAxis range = (NumberAxis) plot.getRangeAxis();

		double min = resultList.get(0).getAccOAO() < resultList.get(0).getAccDDAG() ? (resultList.get(0).getAccOAO() < resultList
				.get(0).getAccADAG() ? resultList.get(0).getAccOAO() : resultList.get(0).getAccADAG()) : (resultList
				.get(0).getAccDDAG() < resultList.get(0).getAccADAG() ? resultList.get(0).getAccDDAG() : resultList
				.get(0).getAccADAG());
		double max = resultList.get(0).getAccOAO() > resultList.get(0).getAccDDAG() ? (resultList.get(0).getAccOAO() > resultList
				.get(0).getAccADAG() ? resultList.get(0).getAccOAO() : resultList.get(0).getAccADAG()) : (resultList
				.get(0).getAccDDAG() > resultList.get(0).getAccADAG() ? resultList.get(0).getAccDDAG() : resultList
				.get(0).getAccADAG());
		for (int i = 1; i < resultList.size(); i++) {
			double low = resultList.get(i).getAccOAO() < resultList.get(i).getAccDDAG() ? (resultList.get(i)
					.getAccOAO() < resultList.get(i).getAccADAG() ? resultList.get(i).getAccOAO() : resultList.get(i)
					.getAccADAG()) : (resultList.get(i).getAccDDAG() < resultList.get(i).getAccADAG() ? resultList.get(
					i).getAccDDAG() : resultList.get(i).getAccADAG());
			double high = resultList.get(i).getAccOAO() > resultList.get(i).getAccDDAG() ? (resultList.get(i)
					.getAccOAO() > resultList.get(i).getAccADAG() ? resultList.get(i).getAccOAO() : resultList.get(i)
					.getAccADAG()) : (resultList.get(i).getAccDDAG() > resultList.get(i).getAccADAG() ? resultList.get(
					i).getAccDDAG() : resultList.get(i).getAccADAG());
			if(min > low){
				min = low;
			}
			if(max < high){
				max= high;
			}
		}

		range.setRange(min-2, max+1);
		//range.setTickUnit(new NumberTickUnit(1));

		final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
		// categoryAxis.setAxisLineVisible(false);
		// categoryAxis.setTickMarksVisible(false);
		categoryAxis.setLowerMargin(0);
		categoryAxis.setMaximumCategoryLabelLines(3);
		setContentPane(chartPanel);
	}

	private DefaultCategoryDataset createDataset(ArrayList<ResultObj> resultList) {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for (int i = 0; i < resultList.size(); i++) {
			String temp = resultList.get(i).getDataName() + "\n" + resultList.get(i).getClassNumber() + "\n"
					+ resultList.get(i).getTestingSize();
			dataset.addValue(resultList.get(i).getAccOAO(), "SVM_OAO", temp);
			dataset.addValue(resultList.get(i).getAccDDAG(), "SVM_DDAG", temp);
			dataset.addValue(resultList.get(i).getAccADAG(), "SVM_ADAG", temp);
		}

		return dataset;
	}

}
